from django.db import models
from tower.models import User
# Create your models here.



# 课程分类表
class CourseCategoryModel(models.Model):
    name = models.CharField(max_length=32)

    class Meta:
        db_table = 'coursecategory'

    def __str__(self):
        return self.name

# 老师表
class TeacherModel(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField(max_length=2000)
    img = models.ImageField(upload_to='avatar/', null=True)
    user = models.ForeignKey(
        to=User,
        on_delete=models.CASCADE,
        related_name='teacher',
        null=True
    )

    class Meta:
        db_table = 'teacher'


# 课程表
class CourseModel(models.Model):
    TAG = (
        (1, '免费'),
        (2, '限免'),
        (3, '会员'),
    )
    name = models.CharField(max_length=32)

    description = models.TextField(max_length=1000)

    img = models.ImageField(upload_to='course/')

    price = models.DecimalField(default=0, decimal_places=2, max_digits=10)
    buy_count = models.IntegerField(default=0)
    follow_count = models.IntegerField(default=0)
    learned_count = models.IntegerField(default=0)

    tag = models.IntegerField(default=1, choices=TAG)
    is_del = models.BooleanField(default=False)

    # 课程分类外键
    category = models.ForeignKey(
        to=CourseCategoryModel,
        related_name='course',
        on_delete=models.CASCADE
    )

    # 老师外键
    teacher = models.ForeignKey(
        to=TeacherModel,
        related_name='course',
        null=True,
        on_delete=models.CASCADE
    )

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'course'


# 课程章节表
class ChapterModel(models.Model):
    name = models.CharField(max_length=200)
    index = models.IntegerField(default=0)
    course = models.ForeignKey(
        to='CourseModel',
        on_delete=models.CASCADE,
        related_name='chapter'
    )

    description = models.TextField(max_length=2000)

    class Meta:
        db_table = 'chapter'
